package com.power.powercash;

import java.awt.Component;
import javax.swing.JTable;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.TableCellRenderer;
import java.util.List;

/**
 *
 * @author Juno
 */
public class ComboBoxCellRenderer implements TableCellRenderer
{

  private JComboBox combo;

  private List list;


  public ComboBoxCellRenderer()
  {
    combo = new JComboBox();
  }

  public ComboBoxCellRenderer(List l)
  {
    list = l;
  }

  public void setList(List l)
  {
    list = l;
  }

  public List getList()
  {
    return list;
  }

  @Override
  public Component getTableCellRendererComponent(
      JTable table, Object value, boolean isSelected,
      boolean hasFocus, int row, int column) {

    combo.setModel(new DefaultComboBoxModel(list.toArray()));
    if(isSelected) {
      combo.setForeground(table.getSelectionForeground());
      combo.setBackground(table.getSelectionBackground());
    } else {
      combo.setForeground(table.getForeground());
      combo.setBackground(table.getBackground());
    }
    combo.setSelectedItem(value);

    return combo;
  }

}
